﻿Imports EstoqueDAL
Imports EstoqueBLL
Class FornecedorCAD
    Dim objBLL As FornecedorBLL
    Dim operacao As Char


#Region "Eventos"
#Region "Evento key press"
    Private Sub eventoKeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TIscrEstadual.KeyPress, MTBCep.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
#End Region

#Region "Load"

    Private Sub FornecedorFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'sta_codigo: 1- ativo, 2 inativo
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo=1 or sta_codigo=2", "sta_descricao")
        objconexao.carregaCombo(CBUf, "Generico.Gen_Estado", "est_uf", "est_uf", "", "")
        If (Not IsNothing(LBCodigo.Text) And LBCodigo.Text.Trim <> "") Then
            objBLL = New FornecedorBLL(Val(LBCodigo.Text))
            CarregaCampos()
            operacao = "A"
        Else : operacao = "I"
        End If
    End Sub
#End Region

#Region "Salvar/Alterar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click

        Try

            objBLL = New FornecedorBLL(Val(LBCodigo.Text), StrConv(LTrim(RTrim(TXTNome.Text)), VbStrConv.ProperCase),
                                     StrConv(LTrim(RTrim(TNomeFantasia.Text)), VbStrConv.ProperCase), StrConv(LTrim(RTrim(TXTEndereco.Text)), VbStrConv.ProperCase),
                                       StrConv(LTrim(RTrim(TXTNum.Text)), VbStrConv.ProperCase), StrConv(RTrim(LTrim(TXTBairro.Text)), VbStrConv.ProperCase), StrConv(LTrim(RTrim(TXTCidade.Text)), VbStrConv.ProperCase),
                                       CBUf.SelectedValue, StrConv(LTrim(RTrim(TXTComplemento.Text)), VbStrConv.ProperCase), MTBCep.Text.Replace(" ", "").Replace("-", ""),
                                       Trim(Temail.Text), Trim(TObs.Text), CBStatus.SelectedValue, Trim(MTBCNPJ.Text).Replace("/", "").Replace("-", "").Replace(" ", "").Replace(",", ""),
                                       Trim(TIscrEstadual.Text), Trim(MTBResidencial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
             Trim(MTBComercial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""), Trim(MTBCelular.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
             Trim(MTBFax.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""))

            If operacao = "I" Then
                If (objBLL.InserirAlterar(objusuarios.Codigo)) Then
                    MessageBox.Show("Fornecedor inserido com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("O fornecedor não pôde ser inserido", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            Else
                If (objBLL.InserirAlterar(objusuarios.Codigo)) Then
                    MessageBox.Show("Fornecedor alterado com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("O fornecedor não pôde ser alterado", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

#Region "KeyDown do Form"
    Private Sub FornecedorCAD_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
#End Region

#Region "Lost Focus"
    Private Sub MTBCNPJ_LostFocus(sender As Object, e As System.EventArgs) Handles MTBCNPJ.LostFocus
        If (MTBCNPJ.Text.Replace(",", "").Replace("-", "").Replace("/", "").ToString.Trim <> "") Then
            Try
                If (IsNothing(objBLL)) Then objBLL = New FornecedorBLL(Val(LBCodigo.Text))
                objBLL.cnpj = Trim(MTBCNPJ.Text).Replace("/", "").Replace("-", "").Replace(" ", "").Replace(",", "")
                If (objBLL.ValidaCNPJ()) Then
                    If (Not objBLL.validareplica()) Then
                        Throw New Exception("CNPJ já cadastrado!")
                    End If
                Else
                    Throw New Exception("CNPJ inválido!")
                    MTBCNPJ.Focus()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
        End If
    End Sub
#End Region
#End Region

#Region "Métodos"
#Region "Carrega Campos"
    Sub CarregaCampos()
        LBCodigo.Text = objBLL.Codigo
        LBTitulo.Text = objBLL.razao
        TNomeFantasia.Text = objBLL.nomefantasia
        TXTComplemento.Text = objBLL.Complemento
        TXTNome.Text = objBLL.razao
        MTBCep.Text = objBLL.Cep
        MTBCNPJ.Text = objBLL.cnpj
        TIscrEstadual.Text = objBLL.inscricao
        TXTBairro.Text = objBLL.Bairro
        TXTCidade.Text = objBLL.Cidade
        TXTEndereco.Text = objBLL.Endereco
        TXTNum.Text = objBLL.Num
        CBUf.SelectedValue = objBLL.Uf
        MTBResidencial.Text = objBLL.telres
        MTBCelular.Text = objBLL.telcel
        MTBComercial.Text = objBLL.telcom
        MTBFax.Text = objBLL.telfax
        TObs.Text = objBLL.Obs
        TEmail.Text = objBLL.Email
        CBStatus.SelectedValue = objBLL.Status
    End Sub
#End Region

#Region "Limpar campos"
    Sub Limpa()
        CBStatus.SelectedValue = 1
        TXTNome.Clear()
        MTBCNPJ.Text = ""
        TIscrEstadual.Clear()
        TXTBairro.Clear()
        TXTCidade.Clear()
        TXTEndereco.Clear()
        TXTNum.Clear()
        CBUf.SelectedValue = ""
        MTBResidencial.Text = ""
        MTBCelular.Text = ""
        MTBComercial.Text = ""
        MTBFax.Text = ""
        TObs.Clear()
        Temail.Clear()
        MTBCep.Text = ""
        TXTComplemento.Clear()
        Me.ObjBLL = Nothing
    End Sub
#End Region
#End Region

End Class